<?php
/* 
 * class ini untuk mengelola listing
 * hanya agen yg bisa mengakses
 */
class Properti_baru extends CI_Controller{
    var $_admin_view = '';

    function  __construct() {
        parent::__construct();
        $this->load->model('model_admin', 'madmin');
        $this->load->model('model_listing', 'mlisting');
        $this->load->model('model_properti_baru', 'mpropbaru');
        $this->load->helper('url','site');
        $this->load->helper('form');
        $this->load->library(array('session','table'));
        $this->config->load('artyproperty');
        $this->_admin_view = $this->config->item('admin_view').'wrapper';
    }

    function index(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
        } else {
            redirect(site_url(),'location');
        }

        $data = array('daftar_listing' => $this->mlisting->get_listing_admin(),
            'page_title' => 'Daftar Properti',
            'page_content' => 'daftar_listing',
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user);
        $this->load->view($this->_admin_view,$data);
    }

    function form_tambah_listing() {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
        }else {
            redirect(site_url(),'location');
        }

        $data = array('isUpdate'=>FALSE,
            'page_title' => 'tambah listing',
            'page_content' => 'tambah_listing',
            'nama' =>'', 'tipe' =>'', 'luas' =>'','jml_kmr_tidur' => '',
            'jml_kmr_mandi'=>'','harga'=>'','sewa_beli'=>'',
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user);
        $this->load->view($this->_admin_view,$data);
    }

    function tambah_listing(){
        //ambil dari method POST
        $reg['sewa_beli'] = $this->input->post('sewa_beli');
        $reg['nama'] = $this->input->post('judul');
        $reg['tipe'] = $this->input->post('tipe');
        $reg['luas_tanah'] = $this->input->post('luas_tanah');
        $reg['luas_bangunan'] = $this->input->post('luas_bangunan');
        $reg['jml_kmr_tidur'] = $this->input->post('jml_kmr_tidur');
        $reg['jml_kmr_mandi'] = $this->input->post('jml_kmr_mandi');
        $reg['jml_kmr_pembantu'] = $this->input->post('jml_kmr_pembantu');
        $reg['harga'] = $this->input->post('harga');        
        $reg['sertifikasi'] = $this->input->post('sertifikasi');
        $reg['tahun_didirikan'] = $this->input->post('tahun_didirikan');
        $reg['furnitur'] = $this->input->post('furnitur');
        $reg['tambahan_info_furnitur'] = $this->input->post('tambahan_info_furnitur');
        $reg['deskripsi'] = $this->input->post('deskripsi');
        $reg['info_sekolah_rs'] = $this->input->post('info_sekolah_rs');
        $reg['area_spesifik'] = $this->input->post('area_spesifik');        
        $reg['info_interior'] = $this->input->post('info_interior');

        $reg['fasilitas_id'] = 4; //ini dummy default fasilitasnya 4 (nanti harus berdasar input ya)
        $reg['user_id'] = 2; //ini dummy default user id nya 1 (nanti harus berdasar input ya)
        $reg['alamat_id'] = 3;  //ini dummy default alamatnya di 3 (nanti harus berdasar input ya)

        //untuk alamat
        //$add['provinsi'] = $this->input->post('provinsi');
        //$add['kota_kab'] = $this->input->post('kota');
        //$add['jalan'] = $this->input->post('jalan');

        //cek validasi masukkan
        if ($this->validation()) {
            $this->db->trans_begin();
            $this->db->insert('properti', $reg);
            if ($this->db->trans_status() == FALSE) {
                $this->db->trans_rollback();
            } else {
                $this->db->trans_commit();
                redirect('admin/listing','location');
            }
        } else {
            $data = array('page_content' => 'tambah_listing',
                            'page_title' => 'Error | tambah listing',
                            'level_user' => $this->session->userdata('level'),
                            'user' => $this->session->userdata('username'),
                            'isLogin' => TRUE,'login'=>'',
                            'nama' =>'', 'tipe' =>'', 'luas' =>'','jml_kmr_tidur' => '',
                            'jml_kmr_mandi'=>'','harga'=>'','sewa_beli'=>'',
                            'password' => '','isUpdate' => FALSE,
                            'note'=>'');
        }

        $this->load->view($this->_admin_view, $data);  //tampilkan datanya
    }

    function form_update_listing(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
        }else {
            redirect(site_url(),'location');
        }
        $properti_id = $this->uri->segment(4);
        $query = $this->mlisting->get_properti(array('properti_id'=>$properti_id));
        $udata = $query->row();
        $data = array(
            'isUpdate'=>TRUE,
            'page_title' => 'update listing',
            'page_content' => 'tambah_listing',
            'properti_id' => $udata->properti_id,
            'nama' => $udata->nama_properti, 'tipe' => $udata->tipe,'luas_bangunan'=> $udata->luas_bangunan,
            'jml_kmr_tidur' => $udata->jml_kmr_tidur, 'jml_kmr_mandi'=>$udata->jml_kmr_mandi,
            'luas_tanah'=> $udata->luas_tanah,
            'harga' => $udata->harga,'sewa_beli' => $udata->sewa_beli,
            'isLogin' => $islogin, 'level_user' => $level,
            'user' => $user);
        $this->load->view($this->_admin_view,$data);
    }

    function update_listing($properti_id=''){
        //ambil dari method POST
        $reg['nama'] = $this->input->post('nama');
        $reg['tipe'] = $this->input->post('tipe');
        $reg['luas'] = $this->input->post('luas');
        $reg['jml_kmr_tidur'] = $this->input->post('jml_kmr_tidur');
        $reg['jml_kmr_mandi'] = $this->input->post('jml_kmr_mandi');
        $reg['harga'] = $this->input->post('harga');
        $reg['sewa_beli'] = $this->input->post('sewa_beli');
        $reg['user_id'] = 1; //ini dummy default user id nya 1 (nanti harus berdasar input ya)
        $reg['alamat_id'] = 3;  //ini dummy default alamatnya di 3 (nanti harus berdasar input ya)

        //cek validasi masukkan
        if ($this->validation()) { echo 'lalala';
            $this->db->trans_begin();
            $this->db->where('properti_id', $properti_id);
            $this->db->update('properti',$reg);
            if ($this->db->trans_status() == FALSE) {
                $this->db->trans_rollback();
            } else {
                $this->db->trans_commit();
                redirect('admin/listing','location');
            }
        } else {
            $properti_id = $this->uri->segment(4);
            $query = $this->mlisting->get_properti(array('properti_id'=>$properti_id));
            $udata = $query->row();
            $data = array('page_content' => 'tambah_listing',
                            'page_title' => 'Error | update listing',
                            'level_user' => $this->session->userdata('level'),
                            'user' => $this->session->userdata('username'),
                            'isLogin' => TRUE,'login'=>'',
                            'nama' => $udata->nama, 'tipe' => $udata->tipe,'luas'=> $udata->luas,
                            'jml_kmr_tidur' => $udata->jml_kmr_tidur, 'jml_kmr_mandi'=>$udata->jml_kmr_mandi,
                            'harga' => $udata->harga,'sewa_beli' => $udata->sewa_beli,
                            'password' => '','isUpdate' => FALSE,
                            'note'=>'');
        }

        $this->load->view($this->_admin_view, $data);  //tampilkan datanya
    }

    function hapus_listing(){
        $listing_id = $this->uri->segment(4);        

        if(!is_null($listing_id) AND $listing_id != 0){
            $this->mlisting->hapus_listing(array('properti_id'=>$listing_id));
        }
        redirect('admin/listing','location');
    }

    public function validation($str=''){
        $this->load->library('form_validation');

        $this->valid = $this->form_validation;

        //konfigurasi aturan2 masukkan valid
        $this->valid->set_rules('judul','judul','trim|required|min_length[4]|max_length[20]');
        $this->valid->set_rules('tipe','tipe','trim|required|min_length[1]|max_length[1]');
        $this->valid->set_rules('luas_tanah','luas_tanah','trim|required|min_length[1]|max_length[5]');
        $this->valid->set_rules('jml_kmr_tidur','jml_kmr_tidur','trim|required|min_length[1]|max_length[3]');
        $this->valid->set_rules('jml_kmr_mandi','jml_kmr_mandi','trim|required|min_length[1]|max_length[3]');
        $this->valid->set_rules('harga','harga','trim|required|min_length[4]|max_length[20]');
        $this->valid->set_rules('sewa_beli','sewa_beli','trim|required|min_length[1]|max_length[2]');
        //end konfigurasi

        return ($this->form_validation->run()==FALSE) ? FALSE : TRUE;
    }

    /**
     * fungsi untuk mengecek duplikasi username pada database
     * @param <type> $username
     * @return <type>
     */
    public function spare_username($username){
        $sql = "SELECT username
                FROM user
                WHERE username=?";
        $query = $this->db->query($sql, array($username));
        $this->valid->set_message('spare_username',"Nama $username sudah ada. Silakan isi dengan username yg lain");
        return ($query->num_rows()==0)? TRUE : FALSE;

    }

    function generate_kota(){
        $kota = array('jakarta' => array('jakarta selatan', 'jakarta pusat', 'jakarta utara'),
            'jateng' => array('tegal', 'semarang', 'purwokerto'),
            'jabar' => array('bandung', 'bogor', 'depok'),
            'default'=>array('-- pilih kota --'));

        $opsi = $_POST['kota'];
        $arr = $kota[$opsi];

        foreach ($arr as $key => $val) {
            echo '<option value="' . $key . '">' . $val . '</option>';
        }
    }
}
?>
